package com.xu.Volatile;

/**
 * volatile 原子性操作
 *  缺点：不能代替锁，无法保证一些复合操作的原子性 比如i++
 */
public class VolatileDemo1 {
    static volatile  int i = 0;

    public static class  PlusTask implements Runnable{

        @Override
        public void run() {
            for (int k=0;k<10000;k++){
                i++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[10];
        for (int i = 0; i < 10; i++) {
            threads[i] = new Thread(new PlusTask());
            threads[i].start();
        }
        for (int i = 0; i <10; i++) {
            threads[i].join();
        }
        System.out.println(i);
    }
}
